import UniformScaleBase from "../NRLib/Component/UniformScaleBase";
import DebugExt from "../NRLib/base/DebugExt";
import WidgetExt, { IAlignParam } from "../NRLib/ccExt/WidgetExt";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ZegoSafeEdge extends UniformScaleBase {

    @property
    private readonly isTop: boolean = false;

    @property
    private readonly defAlign: number = 0;


    private alignParam(v: yy.ISafeArea): IAlignParam {
        let a: IAlignParam = { left: 0, right: 0 };
        if (this.isTop)
            a.top = v.paddingTop;
        else
            a.bottom = v.paddingBottom;
        return a;
    }

    protected doUniformScale() {
        if (!this.active) return;
        const safeArea: yy.ISafeArea = this.safeArea;
        if (!safeArea || safeArea.paddingTop <= 0 && safeArea.paddingBottom <= 0) return;
        WidgetExt.reset(this.node, this.alignParam(safeArea));
    }

    /** 安全边距默认值调整 */
    private get safeArea(): yy.ISafeArea {
        let defaultArea = { paddingTop: 0, paddingBottom: 0, paddingLeft: 0, paddingRight: 0 };
        const config = yy.thirdPlatform.getEnvConfigs().zego;
        if (cc.sys.os == cc.sys.OS_IOS || cc.sys.os == cc.sys.OS_OSX) {
            if (config && config.gameMode == 2 && this.isTop) {
                // ZEGO指定游戏半屏模式，不要上边距
                defaultArea = { paddingTop: this.defAlign, paddingBottom: 30, paddingLeft: 0, paddingRight: 0 }
            }
            else {
                defaultArea = { paddingTop: 60, paddingBottom: 30, paddingLeft: 0, paddingRight: 0 }
            }
        }

        let configArea = null;
        if (config && config.gameConfig && config.gameConfig.specificConfig) {
            const spCfg = config.gameConfig.specificConfig;
            if (spCfg.paddingTop !== undefined || spCfg.paddingBottom !== undefined) {
                configArea = yy.thirdPlatform.getSafeArea();
                DebugExt.debug("[ZegoSafeEdge] 第三方缩放参数：", configArea, ` 处理${this.isTop ? "顶" : "底"}部`);
            }
        }
        return configArea || defaultArea;
    }
}
